#include "gm3d.h"

void GM3D::AddRegularBlock(modelist para_list){
	double xs,xe,xmin,xmax;
	double ys,ye,ymin,ymax;
	double zs,ze,zmin,zmax;
	bool model_added = false;

	std::stringstream tmp_ss;
	gctl::str2ss(para_list.mod_para, tmp_ss, "/");
	tmp_ss >> xs >> xe >> ys >> ye >> zs >> ze;

	if (tmp_ss.fail())
		throw GM3D_INVALID_MODEL_PARAMETER;

	xmin = GCTL_MIN(xs,xe); xmax = GCTL_MAX(xs,xe);
	ymin = GCTL_MIN(ys,ye); ymax = GCTL_MAX(ys,ye);
	zmin = GCTL_MIN(zs,ze); zmax = GCTL_MAX(zs,ze);

	if (para_list.val_type == "replace")
	{
		for (int i = 0; i < model_num_; i++){
			if (model_cube_[i].cen.x >= xmin && model_cube_[i].cen.x <= xmax &&
				model_cube_[i].cen.y >= ymin && model_cube_[i].cen.y <= ymax &&
				model_cube_[i].cen.z >= zmin && model_cube_[i].cen.z <= zmax){
				model_block_val_[i] = para_list.mod_value; //注意重复赋值的块体会覆盖
				model_added = true;
			}
		}
	}
	else if (para_list.val_type == "add")
	{
		for (int i = 0; i < model_num_; i++){
			if (model_cube_[i].cen.x >= xmin && model_cube_[i].cen.x <= xmax &&
				model_cube_[i].cen.y >= ymin && model_cube_[i].cen.y <= ymax &&
				model_cube_[i].cen.z >= zmin && model_cube_[i].cen.z <= zmax){
				if (model_block_val_[i] == GCTL_BDL_MAX)
					model_block_val_[i] = para_list.mod_value; //注意重复赋值的块体会覆盖
				else
					model_block_val_[i] += para_list.mod_value;
				model_added = true;
			}
		}
	}
	else if (para_list.val_type == "erase")
	{
		for (int i = 0; i < model_num_; i++){
			if (model_cube_[i].cen.x >= xmin && model_cube_[i].cen.x <= xmax &&
				model_cube_[i].cen.y >= ymin && model_cube_[i].cen.y <= ymax &&
				model_cube_[i].cen.z >= zmin && model_cube_[i].cen.z <= zmax){
				model_block_val_[i] = GCTL_BDL_MAX; //注意重复赋值的块体会覆盖
				model_added = true;
			}
		}
	}
	else throw GM3D_INVALID_MODEL_VAL_TYPE;

	if (!model_added)
		throw GM3D_NOMODEL_ADDED;
	return;
}